﻿using blackhouse.BlackApart.WebNemo;
using blackhouse.mojoPortalExtend;
using blackhouse.mojoPortalExtend.Urls;
using mojoPortal.Business;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.Linq;
using mojoPortal.Business.WebHelpers;
using NemoTraslate = blackhouse.WebNemo.Business.Translate;

namespace blackhouse.StareKino.Web.Navigation {
    internal class ApartmentsUrlEnsurance {

        #region Fields

        private int siteId;
        private CultureInfo siteCulture;
        private IEnumerable<NemoApartmentType> apartments;
        private NemoTraslate.Translator translator;
        private PageSettings page;
        private int urlColumnId;

        #endregion

        #region Constructors

        public ApartmentsUrlEnsurance() {
            this.siteId = CacheHelper.GetCurrentSiteSettings().SiteId;

            if (this.siteId > 1) {
                string siteCultureName = ConfigurationManager.AppSettings["site" + siteId + "culture"];
                if (String.IsNullOrEmpty(siteCultureName)) throw new NotImplementedException("Site " + siteId + " has no definition for culture. Add key 'site" + siteId + "culture' to appSettings with culture name as key");
                this.siteCulture = new CultureInfo(siteCultureName);

                this.apartments = NemoApartmentType.GetAll(siteCulture).Cast<NemoApartmentType>();

                var pageProvider = new PageProvider();
                this.page = pageProvider.FindCmsPageByFunctionality("apartments", siteId);

                var group = NemoApartmentType.GetNemoGroup();
                this.translator = new NemoTraslate.Translator(group, CultureInfo.GetCultureInfo("pl-PL"));
                this.urlColumnId = group.Columns["page_url"].ColumnId;
            }
        }

        #endregion

        public void EnsureApartmentsUrl() {
            if (siteId == 1) return; // dla podstawowego języka nie ustawiamy automatycznie adresów URL

            foreach (NemoApartmentType apartment in apartments) {
                var cmsUrl = new ApartmentUrlEnsuranceCmsSupport(
                    translator,
                    this.siteId,
                    siteCulture,
                    urlColumnId,
                    apartment,
                    page);
                var urlProvider = new UrlSupportProvider(cmsUrl);
                urlProvider.Save();
            }
        }

        private class ApartmentUrlEnsuranceCmsSupport : UrlSupport {

            #region Fields

            private readonly NemoTraslate.Translator translator;
            private readonly int siteId;
            private readonly CultureInfo siteCulture;
            private readonly int urlColumnId;
            private readonly NemoApartmentType apartment;
            private string oryginalNewUrl = String.Empty;
            private int oryginalAdjustIdx;

            #endregion

            #region Properties

            public override int DirectSiteId {
                get { return this.siteId; }
            }

            #endregion

            #region Constructors

            public ApartmentUrlEnsuranceCmsSupport(
                NemoTraslate.Translator translator,
                int siteId,
                CultureInfo siteCulture,
                int urlColumnId,
                NemoApartmentType apartment,
                PageSettings apartmentsPage
            ) {
                this.translator = translator;
                this.siteId = siteId;
                this.siteCulture = siteCulture;
                this.urlColumnId = urlColumnId;
                this.apartment = apartment;

                this.OldUrl = this.translator.GetText(this.siteCulture, this.urlColumnId, this.apartment.Id);
                this.NewUrl = "~/" + apartment.Name.CleanStringForUrl();
                this.RealUrl = "~/Apartment.aspx?id=" + apartment.Id;
                this.Guid = apartment.Guid;
                if (apartmentsPage != null)
                    this.RealUrl = this.RealUrl + "&pageId=" + apartmentsPage.PageId;
            }

            #endregion

            #region Methods

            public override void ChangeToOtherNewUrl() {
                if (String.IsNullOrEmpty(this.oryginalNewUrl))
                    this.oryginalNewUrl = this.NewUrl;
                this.oryginalAdjustIdx++;
                this.NewUrl = this.oryginalNewUrl + "-" + this.oryginalAdjustIdx;
            }

            public override void Save() {
                this.translator.SetText(this.siteCulture, this.urlColumnId, this.apartment.Id, this.NewUrl);
            }

            #endregion

        }


    }
}